<?php

namespace App\Service\NotifyLog;


use App\Library\Common;
use App\Model\FlyExpressTipsLog;

class ExpressLogService
{

//    信息状态
    const ORDER_STATUS = [
        '1' => '订单已发货',
        '2' => '订单已签收、完成',
        '3' => '订单即将自动关闭',
        '4' => '订单即将自动收货确认',
        '5' => '退货退款通过审核',
        '6' => '退货退款审核驳回',
        '7' => '退货退款完成',
        '8' => '仅退款完成',
        '9' => '仅退款驳回',
    ];


    /**
     * Notes:
     * @param int $admin_id
     * @param string $is_read 是否读取1是 0否
     * @param int $type 1是用户  2是商铺
     * @param int $up_time 1更新消息  0不更新
     * @return mixed
     * Date: 2022/8/23
     * Time:17:31
     * Auth: zdl
     */
    public function list(int $admin_id,$is_read='',$type=1,$up_time=0)
    {
        $where = [];
        $e_model =  New FlyExpressTipsLog();
        if($type==1){
            if($is_read!==''){
                $where['user_is_read'] = $is_read;
            }
            $where['user_id'] = $admin_id;
            if($up_time==1){
                $e_model
                    ->where($where)
                    ->where('shop_is_read','0')
                    ->update(['user_is_read'=>1,'user_read_time'=>Common::date_time(2)]);
            }
        }else{
            if($is_read!==''){
                $where['shop_is_read'] = $is_read;
            }
            $where['shop_id'] = $admin_id;
            if($up_time==1){
                $e_model
                    ->where($where)
                    ->where('shop_is_read','0')
                    ->update(['shop_is_read'=>1,'shop_read_time'=>Common::date_time(2)]);
            }
        }

        return $e_model
            ->where($where)
            ->with('order_info')
            ->orderBy('id','DESC');
    }

    public function info($id,$admin_id,$type=1){

        $e_model =  New FlyExpressTipsLog();
        $info = $e_model->where('id',$id)->first();
        if($type==1){
            $info->user_read_time = Common::date_time(2);
            $info->user_is_read =1;
            $info->save();
        }else{
            $info->shop_read_time = Common::date_time(2);
            $info->shop_is_read =1;
            $info->save();
        }
        return $info;
    }

    /**
     * Notes:插入物流消息
     * @param int $order_id
     * @param int $user_id
     * @param int $shop_id
     * @param int $order_type
     * @param int $order_status
     * @return int
     * Date: 2022/8/25
     * Time:13:13
     * Auth: zdl
     */
    public function insert_log(int $order_id,int $user_id,int $shop_id,int $order_type=1,int $order_status=1){
        $e_model =  New FlyExpressTipsLog();
        return  $e_model->insertGetId([
            'order_id'=>$order_id,
            'user_id'=>$user_id,
            'shop_id'=>$shop_id,
            'order_type'=>$order_type,
            'created_at'=>Common::date_time(2),
            'order_status'=>$order_status,
        ]);
    }

    public function count_size($admin_id,$type=1){
        $e_model =  New FlyExpressTipsLog();
        if($type==1){
            return  $e_model
                ->where('user_id',$admin_id)
                ->where('user_is_read',0)
                ->count('id');
        }else{
            return $e_model
                ->where('shop_id',$admin_id)
                ->where('user_is_read',0)
                ->count('id');
        }
    }

}